package com.yinp.proapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.yinp.proapp.R;
import com.yinp.proapp.utils.AppUtils;

public class TriangleLinearLayout extends LinearLayout {
    public TriangleLinearLayout(Context context) {
        this(context, null);
    }

    public TriangleLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TriangleLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }

    Paint paint1;//画类容
    Paint paint2;//画三角形
    Path path;

    private int triangleWidth, triangleHeight;//三角形的宽高
    private int contentColor, triangleColor;
    private int contentCorners;//圆角
    private int trianglePosition;//三角形所处位置
    private int distance;//跟着三角形位置来的

    private int paddingStart;
    private int paddingEnd;
    private int paddingTop;
    private int paddingBottom;
    private int padding = -1;

    private boolean isFirst = false;//只加载一次避免多次重复加载

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TriangleLinearLayout);

        triangleWidth = ta.getInteger(R.styleable.TriangleLinearLayout_triangle_width, 8);
        triangleHeight = ta.getInteger(R.styleable.TriangleLinearLayout_triangle_height, 8);
        triangleColor = ta.getColor(R.styleable.TriangleLinearLayout_triangle_color, 0);
        trianglePosition = ta.getInteger(R.styleable.TriangleLinearLayout_triangle_position, 1);
        distance = ta.getInteger(R.styleable.TriangleLinearLayout_distance, 0);

        paddingStart = ta.getInteger(R.styleable.TriangleLinearLayout_tllPaddingStart, 4);
        paddingEnd = ta.getInteger(R.styleable.TriangleLinearLayout_tllPaddingEnd, 4);
        paddingTop = ta.getInteger(R.styleable.TriangleLinearLayout_tllPaddingTop, 4);
        paddingBottom = ta.getInteger(R.styleable.TriangleLinearLayout_tllPaddingBottom, 4);
        padding = ta.getInteger(R.styleable.TriangleLinearLayout_tllPadding, -1);

        contentColor = ta.getColor(R.styleable.TriangleLinearLayout_content_color, 0);
        contentCorners = ta.getInteger(R.styleable.TriangleLinearLayout_content_corners, 0);

        ta.recycle();

        triangleWidth = AppUtils.dpToPx(triangleWidth);
        triangleHeight = AppUtils.dpToPx(triangleHeight);
        contentCorners = AppUtils.dpToPx(contentCorners);
        distance = AppUtils.dpToPx(distance);

        paddingStart = AppUtils.dpToPx(paddingStart);
        paddingEnd = AppUtils.dpToPx(paddingEnd);
        paddingTop = AppUtils.dpToPx(paddingTop);
        paddingBottom = AppUtils.dpToPx(paddingBottom);
        padding = AppUtils.dpToPx(padding);
        if (triangleColor == 0) {
            triangleColor = contentColor;
        }
        if (contentColor == 0) {
            contentColor = triangleColor;
        }
        if (padding != -1) {
            paddingStart = padding;
            paddingEnd = padding;
            paddingTop = padding;
            paddingBottom = padding;
        }
        setWillNotDraw(false);
        setBackgroundResource(R.color.transparent);
        setPadding();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!isFirst) {
            init2();
            isFirst = true;
        }
    }

    private void init2() {
        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        paint1.setColor(contentColor);
        paint1.setStyle(Paint.Style.FILL);

        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        paint2.setColor(triangleColor);
        paint2.setStyle(Paint.Style.FILL);

        path = new Path();
        switch (trianglePosition) {
            case 1:
                editX(true);
                break;
            case 2:
                editX(false);
                break;
            case 3:
                editYLeft(true);
                break;
            case 4:
                editYRight(true);
                break;
        }
    }

    private void setPadding() {
        switch (trianglePosition) {
            case 1:
            case 2:
                paddingTop += triangleHeight;
                break;
            case 3:
                paddingStart += triangleHeight;
                break;
            case 4:
                paddingEnd += triangleHeight;
                break;
        }

        setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);
    }

    //    在x轴上变化
    private void editX(boolean plus) {
        if (plus) {
            path.moveTo(distance, triangleHeight);
            path.lineTo(distance + triangleWidth / 2, 0);
            path.lineTo(distance + triangleWidth, triangleHeight);
        } else {
            path.moveTo(getWidth() - distance, triangleHeight);
            path.lineTo(getWidth() - distance - triangleWidth / 2, 0);
            path.lineTo(getWidth() - distance - triangleWidth, triangleHeight);
        }
        path.close();
    }

    /**
     * 在y轴上变化
     *
     * @param plus
     */
    private void editYLeft(boolean plus) {
        if (plus) {
            path.moveTo(0, distance);
            path.lineTo(triangleHeight, distance + triangleWidth / 2);
            path.lineTo(triangleHeight, distance - triangleWidth / 2);
        } else {

        }
        path.close();
    }

    private void editYRight(boolean plus) {
        if (plus) {
            path.moveTo(getWidth(), distance);
            path.lineTo(getWidth() - triangleHeight, distance + triangleWidth / 2);
            path.lineTo(getWidth() - triangleHeight, distance - triangleWidth / 2);
        } else {

        }
        path.close();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(path, paint2);
        switch (trianglePosition) {
            case 1:
            case 2:
                canvas.drawRoundRect(0, triangleHeight, getWidth(), getHeight(), contentCorners, contentCorners, paint2);
                break;
            case 3:
                canvas.drawRoundRect(triangleHeight, 0, getWidth(), getHeight(), contentCorners, contentCorners, paint1);
                break;
            case 4:
                canvas.drawRoundRect(0, 0, getWidth() - triangleHeight, getHeight(), contentCorners, contentCorners, paint1);
                break;
        }
    }
}
